function openEditDsWindow(title) {
    var data 
    if (Ext.getCmp('dsGrid').getSelection().length>0 && title!="增加数据源") {
        data = Ext.getCmp('dsGrid').getSelection()[0].data
    } 
    console.log(data)
    var dsEditWindow = Ext.create('Ext.window.Window', {
        title: title,
        iconCls:'x-fa fa-database',
        width: 350,
        modal:true,
        items:[
            {
                xtype:'form',
                width:'100%',
                height:'100%',
                border:false,
                items:[
                    {
                        xtype: 'hiddenfield',
                        id:'dsId',
                        value:data==null?"":data.id
                    },
                    {
                        xtype: 'textfield',
                        id:'dsName',
                        labelAlign:'right',
                        allowBlank:false,
                        fieldLabel:'名称',
                        value:data==null?"":data.name
                    },
                    {
                        xtype: 'combobox',
                        labelAlign:'right',
                        id:'dsType',
                        editable:false,
                        allowBlank:false,
                        store:Ext.create('Ext.data.Store', {
                            data : [
                                {"id":"MySql", "text":"MySql"},
                                {"id":"SqlServer", "text":"SqlServer"},
                                {"id":"Oracle", "text":"Oracle"}
                            ]
                        }),
                        fieldLabel:'数据库类型',
                        value:data==null?"MySql":data.ds_type
                    },
                    {
                        xtype: 'textfield',
                        id:'dsIp',
                        labelAlign:'right',
                        allowBlank:false,
                        fieldLabel:'IP',
                        value:data==null?"":data.ip
                    },
                    {
                        xtype: 'textfield',
                        labelAlign:'right',
                        id:'dsPort',
                        allowBlank:false,
                        fieldLabel:'端口',
                        value:data==null?"":data.port
                    },
                    {
                        xtype: 'textfield',
                        labelAlign:'right',
                        id:'dsDb',
                        //emptyText:'请输入数据库的名称',
                        allowBlank:false,
                        fieldLabel:'数据库',
                        value:data==null?"":data.db
                    },
                    {
                        xtype: 'textfield',
                        labelAlign:'right',
                        allowBlank:false,
                        id:'dsUserLoginId',
                        fieldLabel:'用户名',
                        value:data==null?"":data.userloginid
                    },
                    {
                        xtype: 'textfield',
                        labelAlign:'right',
                        id:'dsPassword',
                        allowBlank:false,
                        fieldLabel:'密码',
                        value:data==null?"":data.userpassword
                    }
                ],
                buttons: [
                    {
                        xtype: 'button',
                        text: '取消',
                        handler: function() {
                             dsEditWindow.close()
                        }
                    },
                    {
                         xtype: 'button',
                         text: '确定',
                         handler: function() {

                            var ip = Ext.getCmp("dsIp").getValue()
                            var dbType = Ext.getCmp("dsType").getValue()
                            var port = Ext.getCmp("dsPort").getValue()
                            var db = Ext.getCmp("dsDb").getValue()
                            var userLoginId = Ext.getCmp("dsUserLoginId").getValue()
                            var password = Ext.getCmp("dsPassword").getValue()
                            var name = Ext.getCmp("dsName").getValue()
                            var id = Ext.getCmp("dsId").getValue()
                           
                            Ext.Ajax.request({
                                url: "../datasource/save",
                                params: {
                                    id:id,
                                    ip:ip,
                                    dbType:dbType,
                                    port:port,
                                    db:db,
                                    userLoginId:userLoginId,
                                    password:password,
                                    name:name,
                                },
                                success:function(response, opts) {
                                    dsEditWindow.close()
                                    dataSourceStore.load()
                                    Ext.getCmp("dataSourceId").getStore().load()
                                }
                            })

                           
                         }
                    }
                ]
            }
        ]
    })
    dsEditWindow.show()
}

var dataSourceStore = Ext.create('Ext.data.Store', {
    autoLoad: true,
    pageSize: 111,
    remoteSort: true,
    remoteFilter:true,
    //sorters: [{property:"a.id",direction:"DESC"}],
    listeners:{
        beforeload:function() {
            
        }
    },
    proxy: {
        type: 'ajax',
        url : '../datasource/store',
        reader: {
            type: 'json',
            root: 'results',
            totalProperty: 'total'
        }
    }
});
var dataSourceWindow = Ext.create('Ext.window.Window', {
    title: '数据源',
    iconCls:'x-fa fa-database',
    height: 500,
    width: 800,
    layout: 'fit',
    modal:true,
    closeAction:'hide',
    items: {  
        xtype: 'grid',
        id:'dsGrid',
        store:dataSourceStore,
        viewConfig: {
            stripeRows: true,
            enableTextSelection: true
        },
        tbar: [
            { 
                xtype: 'button', text: '增加',iconCls:'x-fa fa-plus',
                handler:function() {
                    openEditDsWindow('增加数据源')
                    
                } 
            },
            { 
                xtype: 'button', text: '修改',iconCls:'x-fa fa-pencil',
                handler:function() {
                    openEditDsWindow('修改数据源')
                   
                } 
            },
            { 
                xtype: 'button', text: '删除',iconCls:'x-fa fa-minus',
                handler:function() {
                    var id = Ext.getCmp('dsGrid').getSelection()[0].data.id
                    Ext.Ajax.request({
                        url: "../datasource/delete",
                        params: {id:id},
                        success:function(response, opts) {
                            dataSourceStore.load()
                        }
                    })

                }   
            },
            { 
                xtype: 'button', text: '测试',iconCls:'x-fa fa-minus',
                handler:function() {
                    var id = Ext.getCmp('dsGrid').getSelection()[0].data.id
                    Ext.Ajax.request({
                        url: "../datasource/testconnection",
                        params: {id:id},
                        success:function(response, opts) {
                            var res = Ext.JSON.decode(response.responseText)
                            if(res.success) {
                                alert("连接成功")
                            } else {
                                alert("连接失败:"+res.message)
                                console.log(res.message)
                            }
                        }
                    })
                }   
            }
        ],
        columns: [
            { text: 'ID', dataIndex: 'id',width:50 },
            { text: '名称', dataIndex: 'name',width:150 },
            { text: '类型', dataIndex: 'ds_type',width:150 },
            { text: 'IP', dataIndex: 'ip',width:150 },
            { text: '端口', dataIndex: 'port',width:90 },
            { text: '数据库', dataIndex: 'db',width:90 },
            { text: '用户名', dataIndex: 'userloginid',flex:1 }/*,
            { text: '密码', dataIndex: 'userpassword' }*/
        ],
        height: 500,
        width: '100%',
        border: false
    }
})